using System;
using System.Collections.Generic;
using System.Text;

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Evolution.Engine.Managers;


namespace Evolution.Engine.Cameras
{
    public class ThirdpersonCamera : Camera
    {
        private float _Lag;

        public float Lag { get { return _Lag; } set { _Lag = value; } }

        public ThirdpersonCamera(string name, Vector3 newCameraOffset)
            : base(name)
        {
            _Type = CameraType.ThirdPerson;
            _Offset = newCameraOffset;
            SetViewport(GameEngine.Instance.Device.Viewport);
        }

        public override void Update(GameTime gameTime)
        {
            Vector3 myUp = Vector3.Up; //Vector3.Transform(Vector3.Up, Matrix.CreateFromQuaternion(_Rotation));
            Vector3 myTranformed = Vector3.Transform(_Offset, Matrix.CreateFromQuaternion(_Rotation));
            _Position = Vector3.Transform(myTranformed, Matrix.CreateTranslation(_RevolveTarget));

            if (SceneManager.Instance.Terrain != null)
            {
                float campos = SceneManager.Instance.Terrain.GetHeightAt(_Position) + 0.1f;

                if (_Position.Y < campos)
                {
                    _Position = new Vector3(
                        _Position.X,
                        campos,
                        _Position.Z
                    );
                }
            }

            _View = Matrix.CreateLookAt(_Position, _RevolveTarget, myUp);

            CreateReflectionMatrix();

            myFrustum = new BoundingFrustum(_View * _Projection);
        }
    }
}
